

package com.hazelcast.sql.impl;

import com.hazelcast.internal.serialization.ReflectionClassNameFilter;
import com.hazelcast.jet.impl.exception.JetDisabledException;
import com.hazelcast.jet.impl.util.Util;
import com.hazelcast.sql.HazelcastSqlException;
import com.hazelcast.sql.SqlResult;
import com.hazelcast.sql.SqlStatement;
import com.hazelcast.sql.impl.security.SqlSecurityContext;
import com.hazelcast.sql.impl.state.QueryClientStateRegistry;

import javax.annotation.Nonnull;
import java.util.UUID;

public class MissingSqlService implements InternalSqlService {

    private final UUID localMemberId;
    private final boolean isJetEnabled;

    public MissingSqlService(UUID localMemberId, boolean isJetEnabled) {
        this.localMemberId = localMemberId;
        this.isJetEnabled = isJetEnabled;
    }

    @Nonnull
    @Override
    public SqlResult execute(@Nonnull SqlStatement statement) {
        throw throwDisabled();
    }

    @Override
    public void start() {
    }

    @Override
    public void reset() {
    }

    @Override
    public void shutdown() {
    }

    @Override
    public void closeOnError(QueryId queryId) {
        // We still want to send an exception to the client, so, just leave method impl empty.
    }

    @Override
    public SqlResult execute(@Nonnull SqlStatement statement, SqlSecurityContext securityContext) {
        throw throwDisabled();
    }

    @Override
    public SqlResult execute(@Nonnull SqlStatement statement, SqlSecurityContext securityContext, QueryId queryId) {
        throw throwDisabled();
    }

    @Override
    public SqlResult execute(@Nonnull SqlStatement statement, SqlSecurityContext securityContext, QueryId queryId, boolean skipStats) {
        throw throwDisabled();
    }

    @Override
    public QueryClientStateRegistry getClientStateRegistry() {
        throw throwDisabled();
    }

    @Override
    public long getSqlQueriesSubmittedCount() {
        return 0;
    }

    @Override
    public long getSqlStreamingQueriesExecutedCount() {
        return 0;
    }

    @Override
    public String mappingDdl(String name) {
        throw throwDisabled();
    }

    @Override
    public ReflectionClassNameFilter getReflectionClassNameFilter() {
        throw throwDisabled();
    }

    private RuntimeException throwDisabled() {
        if (!isJetEnabled) {
            throw new JetDisabledException(Util.JET_IS_DISABLED_MESSAGE);
        }
        throw new HazelcastSqlException(localMemberId, SqlErrorCode.GENERIC, "Cannot execute SQL query because \"hazelcast-sql\" module is not on the classpath", null, null);
    }

    @Override
    public Object getOptimizer() {
        throw throwDisabled();
    }
}
